# -*-coding: Utf-8 -*-
# @File : middlewares .py
# author: Chimengmeng
# blog_url : https://www.cnblogs.com/dream-ze/
# Time：2024/7/2

from django.utils.deprecation import MiddlewareMixin
from pybloom_live import ScalableBloomFilter, BloomFilter
from apps.backend.models import User
import django_redis

redis_conn = django_redis.get_redis_connection()
# 可自动扩容的布隆过滤器
bloom = ScalableBloomFilter(initial_capacity=100, error_rate=0.001)


class ControlRequestMiddle(MiddlewareMixin):
    def process_request(self, request):
        for username in User.objects.all().values_list("username"):
            bloom.add(username[0])

        setattr(request, "bloom", bloom)
        setattr(request, "redis_conn", redis_conn)
